package com.xh.shuati.ai.tools;

import cn.hutool.core.io.FileUtil;
import com.xh.shuati.constant.FileConstant;
import dev.langchain4j.agent.tool.P;
import dev.langchain4j.agent.tool.Tool;

import java.io.File;
import java.nio.charset.StandardCharsets;

public class FileOperationTool {

    private final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";

    @Tool(name = "readfileOperationTool", value = "Read content from a file")
    public String readFile(@P(value = "Name of the file to read") String fileName) {
        String filePath = FILE_DIR + File.separator + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        }catch (Exception e){
            return "Error reading file: " + e.getMessage();
        }
    }

    @Tool(name = "writefileOperationTool", value = "Write content to a file")
    public String writeFile(@P(value = "Name of the file to write") String fileName,
                            @P(value = "Content to write to the file") String content) {
        String filePath = FILE_DIR + File.separator + fileName;

        try {
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeString(content, filePath, StandardCharsets.UTF_8);
            return "File written successfully to: "+ filePath;
        }catch (Exception e){
            return "Error writing file: " + e.getMessage();
        }
    }
}
